package com.congee02;

import java.util.*;

public class MinimumTotal_TL {

    public int minimumTotal(List<List<Integer>> triangle) {
        int[][] tri = toArrays(triangle);

        return process(tri, 0, 0);
    }

    static final int K = Integer.MAX_VALUE >>> 1;
    private int process(int[][] tri, int row, int col) {
        if (row == tri.length) {
            return 0;
        }
        if (col > row || col < 0) {
            return K;
        }

        return tri[row][col] + Math.min(
                process(tri, row + 1, col),
                process(tri, row + 1, col + 1)
        );
    }

    private int[][] toArrays(List<List<Integer>> triangle) {
        int[][] ans = new int[triangle.size()][];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = toArray(triangle.get(i));
        }
        return ans;
    }

    private int[] toArray(List<Integer> lst) {
        int[] ans = new int[lst.size()];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = lst.get(i);
        }
        return ans;
    }

    public static void main(String[] args) {
        MinimumTotal_TL s = new MinimumTotal_TL();
        s.minimumTotal(List.of(
                List.of(-1),
                List.of(3, 2),
                List.of(-3, 1, -1)
        ));
    }


}
